#ifndef _COMM_HPP_
#define _COMM_HPP_

#include <iostream>
#include <sys/types.h>
#include <string.h>
#include <stdio.h>
#include <string>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/shm.h>

using namespace std;


#define PATHNAME "."
#define PROJID 0X6666

const int gsize = 4096;
key_t getKey(){
    key_t k = ftok(PATHNAME,PROJID);
    if(k==-1){
        cerr<<"error:"<<errno<<":"<<strerror(errno)<<endl;
        exit(1);
    }
    return k;
}

static int createShmHelper(key_t k,int size,int flag)
{
    int shmid = shmget(k,gsize,flag);
    if(shmid==-1){
        cerr<<"error:"<<errno<<":"<<strerror(errno)<<endl;
        exit(2);
    }
    return shmid;
}

int createShm(key_t k,int size){
    umask(0);
    return createShmHelper(k,size,IPC_CREAT|IPC_EXCL|0666);
}

int getShm(key_t k,int size){
    return createShmHelper(k,size,IPC_CREAT);
}

#endif